package springtestpkg;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.alibaba.fastjson.JSON;
 
import com.google.common.collect.Maps;
import com.sun.jndi.toolkit.url.UrlUtil;
 

 

 
 
/**
 * v4  dep enhance 
 * v3 dir files  add
 * @author attilax
 *
 */
public class SpringUtilV3_prjcli implements ApplicationContextAware, DisposableBean {
	 final static Logger log = Logger.getLogger(SpringUtilV3_prjcli.class);
	public static void main(String[] args) throws InterruptedException {
		// attilax 老哇的爪子  上午11:54:26   2014-5-14 
	//	SpringUtil.cfgFileDir=PathUtil.classPath_hisCommLib()+"/";
//		SpringUtil. locations =new  String[] {  PathUtil.classPath_hisCommLib()+"/"+"IocSrpingCfg.xml"};
//		annoTest bean = (annoTest) getBean("annoTest");
//		bean.someOp(56);
//		System.out.println(bean);
//		System.out.println("--");
		//branchManagerService
	//	SpringUtilV3.cfgFileDir="C:\\Users\\attilax\\Desktop\\springtest_cli";
	
	//	SpringUtilV3.cfgFileDir="C:\\0wkspc\\clinical\\src\\main\\resources\\META-INF\\springtest_cli";	
		
		
	//	IcolSettingDao d=	(IcolSettingDao) SpringUtilV3_prjcli.getBean("icolSettingDao");
		
		
		
//		SpringUtilV3_prjcli.cfgFileDir="C:\\0wkspc\\clis413\\clinical\\springtest_cli";	
//		SpringUtilV3_prjcli.setLocations(cfgFileDir,"applicationContext-datasource.xml,onehis-dubbo.xml");
//		IcolSettingDao d=	(IcolSettingDao) SpringUtilV3_prjcli.getBean( IcolSettingDao.class);
//	//	gene_barcode_test();
//		ConcurrentMap<Object, Object> newConcurrentMap = Maps.newConcurrentMap();newConcurrentMap.put("key", "key1");
//		Object setting = d.getSetting(newConcurrentMap);
//		System.out.println(setting);
//		log.info("first read:"+setting);
//		//Thread.sleep(12000);
//		CacheManager CacheManager1=  (CacheManager) SpringUtilV3_prjcli.getBean( CacheManager.class);
//		Cache Cache1=CacheManager1.getCache("Cachename1");
//	//	Cache1.clear();
//		
//		Object setting2 = d.getSetting(newConcurrentMap);
//		log.info("sec read:"+setting);
//		
//		
//		Object setting3 = d.getSetting(newConcurrentMap);
//		log.info("thrd read:"+setting);
//		System.out.println(setting2);

	}

//	private static ModelVo gene_barcode_test() {
//		Map map=Maps.newHashMap();
//		//   map.put("bce03", "login_user1");
//		String cbm01Arr=   "%5B%7B%22vaf25%22%3A%22%22%2C%22vaa01%22%3A%22863234486287081472%22%2C%22rownr%22%3A%220%22%2C%22biw02%22%3A%22%22%2C%22vaf23%22%3A%22%22%2C%22vaf21%22%3A%221%22%2C%22vaf22%22%3A%22%E8%91%A1%E8%90%84%E7%B3%96%E6%B5%8B%E5%AE%9A(%E5%90%84%E7%A7%8D%E9%85%B6%E6%B3%95)%22%2C%22vaf20%22%3A0%2C%22bce01h%22%3A%220%22%2C%22crypt%22%3A0%2C%22bce02a%22%3A%228080%22%2C%22vaf62%22%3A0%2C%22vaf61%22%3A0%2C%22cbm01%22%3A%22980720809997901824%22%2C%22vco01%22%3A%220%22%2C%22lroute%22%3A%220%22%2C%22bbx01%22%3A%223776%22%2C%22vaf18%22%3A0%2C%22vaf17%22%3A0%2C%22vaf34%22%3A0%2C%22vaf35%22%3A%220%22%2C%22vaf36%22%3A%222018-04-02+15%3A50%3A29%22%2C%22vaf37%22%3A%221899-12-30+00%3A00%3A00%22%2C%22vaf30%22%3A%22%22%2C%22vaf31%22%3A%220%22%2C%22vaf32%22%3A%220%22%2C%22vaf33%22%3A%220%22%2C%22bce03h%22%3A%220%22%2C%22vaf71%22%3A%222018-04-02+16%3A17%3A16.663%22%2C%22audit_type%22%3A0%2C%22bce03a%22%3A%22%E7%AE%A1%E7%90%86%E5%91%98%22%2C%22vaf01b%22%3A%220%22%2C%22vbgid%22%3A980720810035650600%2C%22vaf01a%22%3A0%2C%22vaf28%22%3A0%2C%22rcurrent%22%3A%220%22%2C%22vaf27%22%3A1%2C%22vaf75%22%3A0%2C%22lsign%22%3A%220%22%2C%22bda01%22%3A%22L%22%2C%22bda01a%22%3A%22L%22%2C%22vaf04%22%3A%221%22%2C%22vaf01%22%3A%22980720809997901826%22%2C%22vaf38%22%3A%221899-12-30+00%3A00%3A00%22%2C%22cbmid%22%3A17%2C%22vaf52%22%3A0%2C%22vaf53%22%3A%22-1%22%2C%22bby01%22%3A0%2C%22vaf54%22%3A0%2C%22bck01c%22%3A%2232%22%2C%22vaf55%22%3A%22%22%2C%22bck01d%22%3A%2232%22%2C%22vaf58%22%3A%220%22%2C%22vaf59%22%3A%221%22%2C%22vaf11%22%3A%222%22%2C%22vaf10%22%3A%221%22%2C%22bck01a%22%3A%2232%22%2C%22bck01b%22%3A%2268%22%2C%22vaf51%22%3A0%2C%22vaf14%22%3A%22%E8%A1%80%E6%B6%B2%22%2C%22bce01b%22%3A%220%22%2C%22bce01a%22%3A%221%22%2C%22vaf06%22%3A%22980645468520321025%22%2C%22vaf07%22%3A%220%22%7D%5D";
//		     cbm01Arr="%5B%7B%22vaf25%22%3A%22%22%2C%22vaf26%22%3A%22%E4%B8%80%E6%AC%A1%E6%80%A7%22%2C%22vaa01%22%3A%22863234486287081472%22%2C%22rownr%22%3A%220%22%2C%22biw02%22%3A%22%22%2C%22vaf23%22%3A%22%22%2C%22vaf21%22%3A%221%22%2C%22vaf22%22%3A%22%E5%9B%9B%E8%82%A2X%E7%BA%BF%E8%AE%A1%E7%AE%97%E6%9C%BA%E4%BD%93%E5%B1%82(CT)%E5%B9%B3%E6%89%AB%22%2C%22vaf20%22%3A0%2C%22bce01h%22%3A%220%22%2C%22crypt%22%3A0%2C%22bce02a%22%3A%228080%22%2C%22vaf62%22%3A0%2C%22vaf61%22%3A0%2C%22cbm01%22%3A%22980726521973055488%22%2C%22vco01%22%3A%220%22%2C%22lroute%22%3A%220%22%2C%22bbx01%22%3A%224053%22%2C%22vaf18%22%3A0%2C%22vaf17%22%3A%221%22%2C%22vaf34%22%3A0%2C%22vaf35%22%3A%220%22%2C%22vaf36%22%3A%222018-04-02+16%3A39%3A15%22%2C%22vaf37%22%3A%221899-12-30+00%3A00%3A00%22%2C%22vaf30%22%3A%22%22%2C%22vaf31%22%3A%220%22%2C%22vaf32%22%3A%220%22%2C%22vaf33%22%3A%220%22%2C%22bce03h%22%3A%220%22%2C%22vaf71%22%3A%222018-04-02+16%3A39%3A58.504%22%2C%22audit_type%22%3A0%2C%22bce03a%22%3A%22%E7%AE%A1%E7%90%86%E5%91%98%22%2C%22vaf01b%22%3A%220%22%2C%22vbgid%22%3A980726522115661800%2C%22vaf01a%22%3A0%2C%22vaf28%22%3A0%2C%22rcurrent%22%3A%220%22%2C%22vaf27%22%3A1%2C%22vaf75%22%3A0%2C%22lsign%22%3A%220%22%2C%22bda01%22%3A%22E%22%2C%22bda01a%22%3A%22E%22%2C%22vaf04%22%3A%221%22%2C%22vaf01%22%3A%22980726521973055490%22%2C%22vaf38%22%3A%221899-12-30+00%3A00%3A00%22%2C%22cbmid%22%3A4%2C%22vaf52%22%3A0%2C%22vaf53%22%3A%22-1%22%2C%22bby01%22%3A0%2C%22vaf54%22%3A0%2C%22bck01c%22%3A%2232%22%2C%22vaf55%22%3A%22%22%2C%22bck01d%22%3A%2232%22%2C%22vaf58%22%3A%220%22%2C%22vaf59%22%3A%221%22%2C%22vaf11%22%3A%222%22%2C%22vaf10%22%3A%221%22%2C%22bck01a%22%3A%2232%22%2C%22bck01b%22%3A%2270%22%2C%22vaf51%22%3A0%2C%22vaf14%22%3A%22%22%2C%22bce01b%22%3A%220%22%2C%22bce01a%22%3A%221%22%2C%22vaf06%22%3A%22980645468520321025%22%2C%22vaf07%22%3A%220%22%7D%5D"; 
//           String vbq1 = "%7B%22vbq27%22%3A%22%22%2C%22vbq28%22%3A%22%22%2C%22vbq29%22%3A%22%22%2C%22vbq30%22%3A%22%22%7D";
//           vbq1="%7B%22vbq27%22%3A%22%22%2C%22vbq28%22%3A%22%22%2C%22vbq29%22%3A%22%22%2C%22vbq30%22%3A%22%22%7D";
//           map.put("cbm01Arr",cbm01Arr        	   );
//		map.put("vbq1", vbq1);
//		URLEncodedUtils.decodeMap(map);
//		
//		CliDoctorImpl cliDoctor=	(CliDoctorImpl) SpringUtilV3_prjcli.getBean(CliDoctorImpl.class);
//		
//           ModelVo nurse_Lis_Bills_Print_batch_rzt = null;
//		try {
//			nurse_Lis_Bills_Print_batch_rzt = cliDoctor.nurse_Lis_Bills_Print_batch((HashMap)map);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println( JSON.toJSONString(nurse_Lis_Bills_Print_batch_rzt, true));
//		return nurse_Lis_Bills_Print_batch_rzt;
//		
//	}

	public static ApplicationContext applicationContext = null;

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		System.out.println("==");
		SpringUtilV3_prjcli.applicationContext = applicationContext;
	}
	
	public void destroy() throws Exception {
		applicationContext = null;
	}

	/**
	 * 获取applicationContext
	 * 
	 * @return applicationContext
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/**
	 * 根据Bean名称获取实例
	 * 
	 * @param name
	 *            Bean注册名称
	 * 
	 * @return bean实例
	 * 
	 * @throws BeansException
	 */
	public  static Object getBean(String name) throws BeansException {
		
		iniAppContext();
		// applicationContext.getBean
		 
		return applicationContext.getBean(name);
	}
	
	public  static Object getBean(Class<?> name)   {
		
		iniAppContext();
		// applicationContext.getBean
		 
		return applicationContext.getBean(name);
	}
 public static  String[] locations ;
//public  static <ati> ati getBean(Class<ati>  cls) throws BeansException {
//		
//		iniAppContext();
//		// applicationContext.getBean
//		return applicationContext.getBean(cls);
//	}
 
 public static String cfgFileDir;
 public  synchronized static void iniAppContext() {
		if(applicationContext==null)
		{
			//applicationContext-*.xml
		 //	applicationContext = new ClassPathXmlApplicationContext("applicationContext-atti.xml");
			
		//	=pathx.classPath()+"/";
			// core.log("--o5d path:"+p);
			 //path:/d:/workspace/Gialen/WebRoot/WEB-INF/classes/
			 if(locations==null)
			    locations =new  String[] {  cfgFileDir+"IocSrpingCfg.xml",     cfgFileDir+"applicationContext-actions.xml",cfgFileDir+ "applicationContext-common.xml",cfgFileDir+ "applicationContext-daos.xml",cfgFileDir+"applicationContext-services.xml",cfgFileDir+"applicationContext-zhoujb.xml"};
			   applicationContext = new FileSystemXmlApplicationContext(locations );
		
		//	applicationContext= new FileSystemXmlApplicationContext(p);
		}
	}
	 
//	public  synchronized static void iniAppContext() {
//		if(applicationContext==null)
//		{
//			//applicationContext-*.xml
//		 //	applicationContext = new ClassPathXmlApplicationContext("applicationContext-atti.xml");
//			
//		//	=pathx.classPath()+"/";
//			// core.log("--o5d path:"+p);
//			 //path:/d:/workspace/Gialen/WebRoot/WEB-INF/classes/
//			 if(locations==null)
//			    locations =new  String[] {  cfgFileDir+"IocSrpingCfg.xml",     cfgFileDir+"applicationContext-actions.xml",cfgFileDir+ "applicationContext-common.xml",cfgFileDir+ "applicationContext-daos.xml",cfgFileDir+"applicationContext-services.xml",cfgFileDir+"applicationContext-zhoujb.xml"};
//			   applicationContext = new FileSystemXmlApplicationContext(locations );
//		
//		//	applicationContext= new FileSystemXmlApplicationContext(p);
//		}
//	}
	private  synchronized static void iniAppContext( String[] locations) {
		if(applicationContext==null)
		{
			//applicationContext-*.xml
		 //	applicationContext = new ClassPathXmlApplicationContext("applicationContext-atti.xml");
			
			String p=pathx_cliprj.classPath()+"/";
		//	 core.log("--o5d path:"+p);
			 //path:/d:/workspace/Gialen/WebRoot/WEB-INF/classes/
		//	   = {p+"applicationContext-actions.xml",p+ "applicationContext-common.xml",p+ "applicationContext-daos.xml",p+"applicationContext-services.xml",p+"applicationContext-zhoujb.xml"};
			   applicationContext = new FileSystemXmlApplicationContext(locations );
		
		//	applicationContext= new FileSystemXmlApplicationContext(p);
		}
	}
	
	


	public static void setLocations(String cfgsDir, String cfgs) {
		String[] a=cfgs.split(",");
	for (int i = 0; i < a.length; i++) {
		a[i]=cfgsDir+"\\"+a[i];
	}
	  	 locations =a;
	  			// ;//new  String[] { cfgsDir+"/"+"IocSrpingCfg.xml"};
		
	}

	/**
	 * by  cfgFileDir n  cfgs.cfgFileDir is pre cfg
	 * @param cfgs
	 */
	public static void setLocations(String cfgs) {
		setLocations(cfgFileDir,cfgs);
		
	}

}